package com.jcgyl.export.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcgyl.export.dao.TborderheadDao;
import com.jcgyl.export.dao.TborderlistDao;
import com.jcgyl.export.entity.Tborderhead;
import com.jcgyl.export.entity.Tborderlist;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.entity.Tbuploadinfo;
import com.jcgyl.export.mapper.TbuploadinfoMapper;
import com.jcgyl.export.model.BaseResponseModel;
import com.jcgyl.export.model.GenerateParameter;
import com.jcgyl.export.mq.MessageSender;
import com.jcgyl.export.util.MessageHelper;
import com.jcgyl.export.util.POIExcelHelper;
import jxl.Cell;

@RestController
@RequestMapping("/order/destination")
public class DestinaionController {
	@Autowired
	private TbuploadinfoMapper tbuploadinfoMapper;
	@Autowired
	private TborderheadDao orderheadDao;
	@Autowired
	private TborderlistDao orderlistDao;
	@Autowired
	private MessageSender messageSender;
	@PostMapping("/send")
	public BaseResponseModel Send(@RequestBody GenerateParameter parameter) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		System.out.println("发送的用户"+user.getUname());
		BaseResponseModel model = new BaseResponseModel();
		int uploadId=parameter.getUploadId();
		System.out.println("上传者ID"+uploadId); 
		try {
			Tbuploadinfo upload = tbuploadinfoMapper.selectByPrimaryKey(uploadId);
			String file_path=upload.getFilePath();
			List<Cell[]> cell_lists=POIExcelHelper.jxlGetExcelColumns(file_path);
			for(Cell[] cells :cell_lists){
				String order_no=cells[0].getContents();
				Tborderhead  order_head=orderheadDao.findOrderheadByOrderId(order_no);
				List<Tborderlist> order_list=orderlistDao.findOrderlistByOrderId(order_no);
				String destination_order_message=MessageHelper.GenerateOrderMessage(order_head, order_list);
				messageSender.send(destination_order_message);
			}
		} catch (Exception e) {
			model.setIsError(true);
			model.setMessage(e.getMessage());
		}
		return model;
	}
}
